home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 18.5 KB | 478 lines | [TEXT/MPS ] |
- // UFileBasedDocument.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __UFILEBASEDDOCUMENT__
- #define __UFILEBASEDDOCUMENT__
-
- // MacApp
-
- #ifndef __GEOMETRY__
- #include "Geometry.h"
- #endif
-
- #ifndef __UDOCUMENT__
- #include "UDocument.h"
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // Constants for the finder CString resources
- //----------------------------------------------------------------------------------------
-
- const ResType kFinderStringType = 'STR ';
- const ResNumber kFinderApplicationNameID = -16396;
- const ResNumber kFinderMessageID = -16397;
-
- //----------------------------------------------------------------------------------------
- // Constants for the script resource
- //----------------------------------------------------------------------------------------
-
- const ResType kScriptInfoRsrcType = 'scpt';
- const ResNumber kScriptInfoRsrcID = 128;
-
- //----------------------------------------------------------------------------------------
- // Constants for the selection resource
- //----------------------------------------------------------------------------------------
-
- const ResNumber kSelectionRsrcID = 1; // rsrc id for selection rsrc
-
- //----------------------------------------------------------------------------------------
- // How to save a document.
- //
- // svtAlways: Always save with SaveViaTemp. Never overwrite the existing file.
- // svtAskUser: Try to SaveViaTemp.If not possible then ask before SaveInPlace.
- // sipAlways: Always save with SaveInPlace. Never create a temporary file.
- // sipAskUser: Always save with SaveInPlace, but only if user confirms.
- //----------------------------------------------------------------------------------------
-
- enum SaveChoice { svtAlways, svtAskUser, sipAlways, sipAskUser };
-
- //----------------------------------------------------------------------------------------
- // Forward and external class declarations.
- //----------------------------------------------------------------------------------------
-
- struct StandardFileReply;
- class TFile;
- class TSaveFileDocCommand;
- class TCloseFileDocCommand;
- #if qContainer
- class TODPartView;
- #endif
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument: Corresponds to a Finder document. Managed document data in files
- // and main memory.
- //----------------------------------------------------------------------------------------
-
- class TFileBasedDocument : public TDocument
- {
- MA_DECLARE_CLASS;
-
- public:
- TFile* fFile; // volume/directory/filename
-
- Boolean fFileExists; // whether a disk file representing this
- // model exists
-
- SaveChoice fHowToSave; // how to save files
-
- OSType fScrapType; // document's private scrap type
-
- TFileBasedDocument();
- // Constructor
-
- void IFileBasedDocument(TFile* itsFile, OSType itsScrapType);
- // Initialization method for TFileBasedDocument.
-
- virtual void DoInitialState();
-
- virtual TFile* DoMakeFile(CommandNumber itsCommandNumber);
- // Default behavior is to gApplication->DoMakeFile. May be overridden to create
- // a file of your own special type.
-
- // virtual TFileHandler* DoMakeFileHandler(TFile* itsFile);
- // // Creates and returns a TFileHandler for this object.
-
- virtual ~TFileBasedDocument();
- // This does not call FreeData by default, since you may need to control the order
- // in which things are freed. Your override of TDocument::Free can call FreeData if
- // convenient.
-
- virtual void RegainControl();
- // Called just after the application has been resumed to allow the
- // document to change its title according to an outside file name change.
-
-
- //------------------------------------------------------------------------------------
- // Opening Documents
- //------------------------------------------------------------------------------------
-
- virtual void AboutToSaveFile(TFile* theSaveFile,
- CommandNumber itsCmd,
- Boolean& makingCopy);
- // Called from file handler right after the SFPutFile (if any) and before deciding
- // between SaveViaTemp & SaveInPlave. Applications can use the name in an alert
- // and/or modify the makingCopy parameter if they do not want to have the document
- // renamed.
-
- virtual Boolean FindDocument(TFile* aFile);
- // True if this file is already open
-
- virtual void DoNeedDiskSpace(TFile* itsFile,
- long& dataForkBytes,
- long& rsrcForkBytes);
- // Bytes required to store SAVE file(s) on disk. When called, both parameters are
- // set to 0 for you.
-
- virtual void DoRead(TFile* aFile, Boolean forPrinting);
- // The default behavior is to just read the printinfo from the document's
- // data-fork by calling DoReadPrintInfo (useful huh?)
-
- virtual void DoReadPrintInfo(TFile* aFile, Boolean forPrinting);
- // The default behavior is to just read the printinfo from the document's
- // data-fork (useful huh?)
-
- #if qAttachable
- virtual void DoReadScript(TFile* aFile, Boolean forPrinting);
- // The default behavior is to just read the script from the resource fork.
- #endif
-
- virtual void DoReadSelection(TFile* aFile, Boolean forPrinting);
- // The default behavior is to just read the selection from the resource fork.
-
- virtual void DoWrite(TFile* aFile, Boolean makingCopy);
- // makingCopy indicates whether this save is for making a copy of the document
- // (vs. a regular Save or Save As); disk-based documents (especially) might
- // optimize their behaviour if this is true.
-
- virtual void DoWriteFinderString(TFile* aFile, Boolean makingCopy);
- // The default behavior is to retrieve the finder message and application name
- // and write the appropriate CString to the document's resource-fork.
-
- virtual void DoWritePrintInfo(TFile* aFile, Boolean makingCopy);
- // makingCopy indicates whether this save is for making a copy of the document
- // (vs. a regular Save or Save As); disk-based documents (especially) might
- // optimize their behaviour if this is true. The default behavior is to just write
- // the printinfo to the document's resource-fork (useful huh?)
-
- #if qAttachable
- virtual void DoWriteScript(TFile* aFile, Boolean makingCopy);
- // makingCopy indicates whether this save is for making a copy of the document
- // (vs. a regular Save or Save As); disk-based documents (especially) might
- // optimize their behaviour if this is true. The default behavior is to just write
- // the script to the document's resource-fork (useful huh?)
- #endif
-
- virtual void DoWriteSelection(TFile* aFile, Boolean makingCopy);
- // makingCopy indicates whether this save is for making a copy of the document
- // (vs. a regular Save or Save As); disk-based documents (especially) might
- // optimize their behaviour if this is true. The default behavior is to just write
- // the selection to the document's resource-fork
-
- virtual void ReadDocument(Boolean forPrinting);
- // Called to read an existing document for display or printing.
-
- virtual void ReadStationery(TFile* itsNewFile);
- // Called to read an existing stationery pad for display.
-
- //------------------------------------------------------------------------------------
- // Saving Documents
- //------------------------------------------------------------------------------------
-
- virtual void GetSaveLocation(CommandNumber itsCommandNumber, CAEDesc& theSaveDesc);
- // Creates an CAEDesc describing where the document would be saved
-
- virtual void DoSave(CommandNumber itsCommandNumber);
- // Handle the cSave, cSaveAs, and cSaveCopy commands
-
- virtual void SaveDocument(CommandNumber itsCommandNumber);
- // Try to save the document to disk
-
- virtual void FileHasBeenSaved(const CStr255& newName);
- // Called when a save successfully completes and we are switching to the new file
- // (ie., not a Save a Copy In).
-
- virtual void SFPutParms(CommandNumber itsCommandNumber,
- CStr255& prompt,
- CStr255& defaultName,
- ResNumber& dlgID,
- CPoint& where,
- ProcPtr& dlgHook,
- ProcPtr& modalFilter,
- Ptr& activeList,
- ProcPtr& activateProc,
- StandardFileReply* reply,
- void*& yourDataPtr);
- // Called to return all the parameters that should be passed to CustomPutFile or
- // SFPutFile. Defaults to prompt gotten from resource file; defaultName not
- // changed;
- // dlgID = sfPutDialogID or putDlgID;
- // where = (-1, -1) or (100, 100);
- // dlgHook = NULL;
- // modalFilter = gMacAppStandardFileFilter;
- // activeList = NULL;
- // activateProc = NULL;
- // yourDataPtr = NULL.
-
-
- //------------------------------------------------------------------------------------
- // Closing Documents
- //------------------------------------------------------------------------------------
-
- virtual TCloseDocCommand* MakeCloseCommand();
- // This method creates a document saving command appropriate to this type
- // of document.
-
- //------------------------------------------------------------------------------------
- // Revert
- //------------------------------------------------------------------------------------
-
- virtual void RevertDocument();
- // If fSaveExists is true then reverts to the file saved on disk, otherwise merely
- // resets the print handler and calls doInitialState. Calls FreeData to free any
- // existing data and commits any currently active command.
-
-
- //------------------------------------------------------------------------------------
- // Miscellaneous
- //------------------------------------------------------------------------------------
-
- virtual void CheckFile(ResNumber rsrcId, short rsrcIndex, Boolean reverting);
- // Check to see if the disk file has changed. If so put up alert phFileChanged
- // after setting ^0 to fTitle and ^1 to the CString specified by the rsrcId and
- // rsrcIndex. If user cancels, signal Failure(noErr, messageCancelled). If reverting
- // is true, then I/ O errors and nonmatching file types will cause a Failure.
- // Otherwise, this is a Save and any I/ O errors and nonmatching file type will be
- // ignored.
-
- virtual void Close();
- // Close a document. Closes the associated file. ??? NOTE: Must never be called
- // for a document related to a view in the Clipboard.
-
- virtual void CloseFile();
- // Called to free in memory data associated with the currently opened document.
- // The default is to call fFile->Close(true) to force the file close.
-
- // virtual TFileHandler* GetFileHandler();
- // // accessor for fFileHandler
-
- virtual Boolean IsChanged();
- // If the document has been changed
-
- virtual void SetTitle(const CStr255& aTitle);
- // Does fFile->SetName to aTitle and calls SetTitleForDoc for each window of the
- // document.
-
- virtual void GetFinderMessage(CStr255& theMessage);
- // Override this method to provide a message for use by the finder if your
- // document is not be opened directly by the user. The default behavior is to set
- // theMessage to an empty CString.
-
- virtual void DoClose(CommandNumber aCommand,
- Boolean useAppleEvent = TRUE);
- // This method creates a document closing command appropriate to this type
- // of document and then posts it. Pass a FALSE if this close is NOT
- // supposed to generate an AppleEvent.
-
- //------------------------------------------------------------------------------------
- // File Handling
- //------------------------------------------------------------------------------------
-
- virtual Boolean FileAlreadyOpen(TFile* aFile);
- // True if the file for the model is already open
-
- virtual OSErr FileChanged(Boolean checkType);
- // Returns noErr if fSaveExists is false or if the file's modification date
- // matches fModDate. If checkType is true then returns errFTypeChanged if the file
- // type has changed. If the file exists but with a different modification date,
- // returns errFileChanged.
-
- virtual Boolean FileExists();
- // Default returns the status of fFileExists
-
- virtual TFile* GetFile();
- // Return the file for this document.
-
- virtual void SetFile(TFile* itsNewFile);
- // Set the file for this document, disposing of the old file if it exists
-
- virtual void GetFileName(CStr63& aName);
- // Return the name of the file
-
- virtual Boolean GetSaveInfo(CommandNumber itsCommandNumber,
- Boolean copyFInfo,
- CInfoPBRec& cInfo);
- // Called to get the desired file info for saving the file. Default copies the
- // info from the current file if fSaveExists AND copyFInfo, otherwise it just sets
- // the file type and creator based on the fields of this. If an error occurs
- // calling the file system, the cInfo record will not be filled in. Returns true
- // if it successfully gets the info from the file.
-
- virtual void GetTempName(CStr63& fileName);
- // Return a temporary name for saving the file. The filename is constructed in two
- // parts: the first part is the document's name, or the application's name if the
- // document is untitled. The second part, appended to the first part, is a pseudo-
- // random number based on the tick count and number of seconds since boot.
-
- virtual void ReadFile(Boolean forPrinting);
- // Called to read an existing file for display or printing.
-
- virtual void RequestFileName(CommandNumber itsCommandNumber,
- Boolean makingCopy,
- TFile* aFile);
- // Asks the user for a filename for the document. Calls SFPutParms
- // and then SFPPutFile. Fails if the user clicks the Cancel button to end the put
- // file dialog.
-
- virtual void SaveFile(CommandNumber itsCommandNumber,
- Boolean askForFilename,
- Boolean copyFInfo,
- Boolean makingCopy);
- // Try to save the document to disk; If askForFilename, put up a Std File box; If
- // makingCopy, then this is for the Save a Copy In command, so we do not rename
- // the document. This method calls FlushVol (which takes care of the requirements
- // of SaveInPlace, and SaveViaTemp.
-
- virtual void SaveInPlace(CommandNumber itsCommandNumber,
- Boolean makingCopy,
- Boolean copyFInfo,
- TFile* itsFile);
- // Called to save a document in place. Default is to delete the existing file if
- // NOT (fDataOpen OR fRsrcOpen). If you have disk based document, you have to
- // decide whether to allow saving in place. makingCopy is true if this save is
- // making a copy of the document (vs. a regular Save or Save As) copyFInfo is true
- // if the file information should be copied to the new file Caller is responsible
- // for calling FlushVol after this.
-
- virtual void SaveViaTemp(CommandNumber itsCommandNumber,
- Boolean makingCopy,
- Boolean copyFInfo,
- TFile* itsFile);
- // Called to save the document by creating a temporary copy of the file and
- // renaming it. MacApp uses this method except when there is not enough disk space
- // available. makingCopy is true if this save is making a copy of the document
- // (vs. a regular Save or Save As) copyFInfo is true if the file information
- // should be copied to the new file Caller is responsible for calling FlushVol
- // after this.
-
- virtual void SetFileName(const CStr63& aName);
- // Default behavior calls fFile->SetName with aName
-
- virtual Boolean ValidateFileSpec();
- // Determine if the file's name has changed and update the document's if so.
-
- //------------------------------------------------------------------------------------
- // Scripting Support
- //------------------------------------------------------------------------------------
-
- virtual long CountContainedObjects(DescType desiredType);
-
- virtual void DoAESave(TAppleEvent* message,
- TAppleEvent* reply);
-
- virtual void DoAEClose(TAppleEvent* message,
- TAppleEvent* reply);
-
- virtual MScriptableObject* GetContainedObject(DescType desiredType,
- DescType selectionForm,
- const CAEDesc& selectionData);
-
- #if qContainer
- //------------------------------------------------------------------------------------
- // Container Application Support
- //------------------------------------------------------------------------------------
-
- public:
- virtual CADocumentRef GetContainer();
- // Overridden to return the container document reference.
-
- void RegisterCAHandlers();
- // Register the callbacks for this newly read document.
-
- virtual void AddODPartView(TODPartView* thePartView);
- //add a view pointer to the list of ODPartViews
-
- virtual void DeleteODPartView(TODPartView* thePartView);
- //remove a view pointer from the list of ODPartViews
-
- TList* fODPartViewList;
- //list of ODPartViews
- private:
- static void MakeDatedUniqueFSSpec(FSSpec* fsSpec);
-
- protected:
- CADocumentRef fContainerDocument;
- CAOffset fContainerOffset;
- CASize fContainerLength;
- WindowPtr fRootWindowRef;
-
- #endif // qContainer
-
- };
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand: Closes a file-based document.
- //----------------------------------------------------------------------------------------
-
- class TSaveFileDocCommand : public TSaveDocCommand
- {
- MA_DECLARE_CLASS;
-
- public:
- TSaveFileDocCommand();
- virtual ~TSaveFileDocCommand();
-
-
- // Perform final command preparation.
-
- void ISaveFileDocCommand(CommandNumber itsCommandNumber,
- TDocument* itsDocument);
- void ISaveFileDocCommand(TDocument* itsDocument,
- TAppleEvent* message,
- TAppleEvent* reply);
- // Initialize the TSaveFileDocCommand procedurally.
-
- virtual void DoIt();
- // Actually perform the document closing.
-
- virtual TAppleEvent* MakeAppleEvent();
-
- protected:
- TFile* fSaveFile;
- };
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand: Closes a file-based document.
- //----------------------------------------------------------------------------------------
-
- class TCloseFileDocCommand : public TCloseDocCommand
- {
- MA_DECLARE_CLASS;
-
- public:
- TCloseFileDocCommand();
- virtual ~TCloseFileDocCommand();
-
-
- // Perform final command preparation.
-
- void ICloseFileDocCommand(CommandNumber itsCommandNumber,
- TDocument* itsDocument);
- void ICloseFileDocCommand(TDocument* itsDocument,
- TAppleEvent* message,
- TAppleEvent* reply);
- // Initialize the TCloseFileDocCommand procedurally.
-
- virtual TAppleEvent* MakeAppleEvent();
- // Make an Apple Event equivalent to the command.
-
- virtual void DoIt();
- // Actually perform the document closing.
-
- protected:
- TFile* fCloseFile;
- CommandNumber fSaveCommand;
- };
-
- #endif // __UFILEBASEDDOCUMENT__
-